// pages/personForCar/personForCar.js
var main = require('../../utils/main.js');
const util = require('../../utils/util.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    startPlace:'',
    termini:'',
    date:'',
    userInfo: '',
    imageUrl: app.globalData.imageUrl,
    isMine:false,
    isWholeMine:false,
    type:1,
    actiItem:[],
    loadMoreTxt:'正在加载数据。。。',
    loadMore:false,
    hasData:true,
    page:1,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    var obj = { '1': '车找人', '2': '人找车', '3': '货找车', '4':'车找货'};
    if (options.isWholeMine){
      this.setData({
        isWholeMine: true,
      })
      wx.setNavigationBarTitle({
        title: '我的行程'
      })
    }
    if (options.type){
      wx.setNavigationBarTitle({
        title: obj[options.type]
      })
      this.setData({
        type: parseInt(options.type),
        actiItem: []
      })
    }
    main.changeInput.apply(this, []);
    wx.showLoading({})
  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  addHistory: function (value) {
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/save',
      data: {
        userId: this.data.userInfo.id,
        serachName: value,
        type: 1,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
      }
    })
  },
  getSeachData(flag){
    var begintime_end='';
    var year = new Date().getFullYear();
    var month= new Date().getMonth()+1;
    var day = new Date().getDate();
    if (month<10){
      month = '0' + month;
    }
    if (this.data.date!=''){
      begintime_end = year + '-' + month + '-' + day;
    }
    var _this = this;
    var param={};
    var url='';
    if (this.data.isWholeMine){
      url = 'freeRidePay/pageTravel';
      param = {
        currentPage: this.data.page,
        pageNumber: '10',
        userId: this.data.userInfo.id
      };
    }else{
      url ='freeRideInfo/page';
      param = {
        currentPage: this.data.page,
        pageNumber: '10',
        placeFrom: this.data.startPlace,
        placeTo: this.data.termini,
        backupA: this.data.date,
        type: _this.data.type,
        category: 1
      };
    }
    wx.request({
      url: app.globalData.httpApiCar + url, //仅为示例，并非真实的接口地址
      data: param,
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        var result = [];
        // if (_this.data.isWholeMine) {
        //   result = res.data.data.pp;
        // } else {
        //   result = res.data.data.pp;
        // }
        result = res.data.data.pp;
        if (result.length<10){
          _this.setData({
            hasData:false
          })
        }else{
          _this.setData({
            hasData: true
          })
        }
        result.forEach(function (ele) {
          
          ele.isStart = false;//未出发
          if (_this.data.isWholeMine) {
            if (new Date().getTime() > new Date(ele.freeRideInfo.backupA.replace(/-/g, "/")).getTime()) {
              ele.isStart = true;
            }
          }else{
            if (new Date().getTime() > new Date(ele.backupA.replace(/-/g, "/")).getTime()) {
              ele.isStart = true;
            }
          }
          ele.time='';
          if (ele.createTime) {
            ele.time = util.getDays(ele.createTime)
          }
          if (ele.freeRideInfo){
            if (ele.freeRideInfo.createTime) {
              ele.freeRideInfo.time = util.getDays(ele.freeRideInfo.createTime)
            }
            if (ele.freeRideInfo.labelArray != undefined && ele.freeRideInfo.labelArray != '') {
              ele.freeRideInfo.mark = ele.freeRideInfo.labelArray.split('/');
            } else {
              ele.freeRideInfo.mark = [];
            }
          }
          if (ele.labelArray != undefined && ele.labelArray != '') {
            ele.mark = ele.labelArray.split('/');
          } else {
            ele.mark = [];
          }
          
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem = _this.data.actiItem.concat(result);
        _this.setData({
          actiItem: _this.data.actiItem,
          loadMore: false
        })
      }
    })
    if (this.data.startPlace != '') {
      this.addHistory(this.data.startPlace);
    }
    if (this.data.termini != '') {
      this.addHistory(this.data.termini);
    }
  },
  showDetail:function(e){
    var url = '/pages/carDetail/carDetail?id=' + e.currentTarget.dataset.item.id;
    if (this.data.isWholeMine){
      var item = e.currentTarget.dataset.item;
      var contactNum = item.freeRideInfo.contactNum;
      if (item.freeRideInfo.userId==this.data.userInfo.id){
        if (item.guestId != this.data.userInfo.id){
          contactNum = item.guestInfo.phone;
        }
        if (item.masterId != this.data.userInfo.id) {
          contactNum = item.masterInfo.phone;
        }
        url = '/pages/carDetail/carDetail?isOrder=true&id=' + item.freeRideInfo.id + '&contactNum=' + contactNum
      }else{
        url = '/pages/carDetail/carDetail?isOrder=true&id=' + item.freeRideInfo.id + '&contactNum=' + contactNum;
      }
    }else{
      // wx.setStorage({
      //   key: 'carDetail',
      //   data: e.currentTarget.dataset.item
      // })
    }
    wx.navigateTo({
      url: url
    })
  },
  bindDateChange: function (e) {//时间选择改变事件
    this.setData({
      date: e.detail.value
    })
  },
  reversal:function(e){
    this.setData({
      startPlace: this.data.termini,
      termini: this.data.startPlace
    })
  },
  seachFun(){//查询点击事件
    this.setData({
      page: 1
    })
    this.getSeachData(true);
  },
  /**
   * 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      actiItem:[],
      page: 1,
      loadSuccess: false
    })
    wx.showLoading({});
    this.getSeachData(true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page:1
    })
    this.getSeachData(true);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    if (that.data.hasData) {
      this.setData({
        loadMoreTxt:'正在加载数据。。。',
        loadMore: true,
        page:this.data.page+1
      });
      this.getSeachData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})